package com.wangsd.web.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.utils.DateUtils;
import com.wangsd.common.utils.OssFileUtils;
import com.wangsd.web.sys.entity.UploadFile;
import com.wangsd.web.sys.service.UploadFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sys/upload")
@Api(value = "upload", tags = "文件通用异步上传")
@Slf4j
public class UploadController extends MyController {

    @Autowired
    private UploadFileService service;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @ApiOperation(value = "上传文件", notes = "上传文件", httpMethod = "POST")
    @RequestMapping(value = "uploadFile", method = RequestMethod.POST)
    public Object uploadFile(@ApiParam(value = "文件", required = true) @PathVariable("file") MultipartFile file) {
        UploadFile uploadFile = null;
        String fileName = DateUtils.getCurDateTimeSSS() + "." + FilenameUtils.getExtension(file.getOriginalFilename());
        try {
            String fileUrl = OssFileUtils.uploadFile(fileName, file.getInputStream());
            uploadFile = service.saveUploadFile(fileName, fileUrl);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return uploadFile;
    }

    @ApiOperation(value = "删除文件")
    @PostMapping("/deleteFile")
    public Result deleteFile(@ApiParam(value = "文件名称", required = true) @RequestParam String fileName) {
        service.deleteByFileName(fileName);
        OssFileUtils.deleteFile(fileName);
        return Result.success();
    }

    @ApiOperation(value = "保存文件")
    @PostMapping("/saveUploadFile")
    public Object saveUploadFileByCustomer(@RequestBody UploadFile uploadFile) {

        service.updateUploadFile(uploadFile);

        return Result.success();
    }

    @ApiOperation(value = "分页查询拥有的文件")
    @PostMapping("/selectPage")
    public Result selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                                       @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                                       @ApiParam(value = "排序字段") @RequestParam(required = false) String sortField,
                                       @ApiParam(value = "排序方式") @RequestParam(required = false) String sortOrder,
                                       UploadFile queryObj) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<UploadFile> list = service.selectPage(queryObj, sortField, sortOrder);
        return Result.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "上架下架")
    @GetMapping("/{id}/updateStatus")
    public Result updateStatus(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        service.updateStatus(id);
        return Result.success();
    }

}
